from fastapi import APIRouter, HTTPException, Depends, Query
from datetime import date
from typing import Optional
from routers.user import get_current_user_id
from crud.user_center.Healty_data import record_user_health_data, get_user_health_data
from pydantic import BaseModel

router = APIRouter(tags=["健康数据"])

class HealthDataInput(BaseModel):
    height: float
    weight: float
    age: int
    gender: int

@router.post("/recordHealthData")
async def record_health_data_route(
    health_data: HealthDataInput,
    user_id: str = Depends(get_current_user_id)
):
    """
    记录用户的健康数据
    """
    try:
        result = record_user_health_data(user_id, health_data.height, health_data.weight, health_data.age, health_data.gender)
        print(result)
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))

@router.get("/getHealthData")
async def get_health_data_route(
    user_id: str = Depends(get_current_user_id),
    target_date: Optional[date] = Query(None, description="目标日期，格式为 YYYY-MM-DD")
):
    """
    获取用户的健康数据
    """
    try:
        if target_date is None:
            target_date = date.today()
        
        result = get_user_health_data(user_id, target_date)
        
        if result is None:
            raise HTTPException(status_code=404, detail="Health data not found for the given date.")
        
        return result
    except Exception as e:
        raise HTTPException(status_code=500, detail=str(e))